package com.example.mq.demo;

import com.example.mq.common.MqException;
import com.example.mq.mqclient.Channel;
import com.example.mq.mqclient.Connection;
import com.example.mq.mqclient.ConnectionFactory;
import com.example.mq.mqserver.core.BasicProperties;
import com.example.mq.mqserver.core.ExchangeType;

import java.io.IOException;

/**
 * 模拟生产者，一般都是独立的服务器程序
 * 这里使用单机程序
 */
public class ProducerDemo {
    public static void main(String[] args) throws IOException, MqException, InterruptedException {
        System.out.println("<------------------生产者启动-------------------->");
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("127.0.0.1");
        connectionFactory.setPort(9999);
        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();

        channel.exchangeDeclare("exchange", ExchangeType.DIRECT,true,false,null);
        channel.queueDeclare("queue",true,false,true,null);
        while(true) {
            System.out.println("----------发送消息---------");
            byte deliverMode = 0x2;
            byte[] body = "hello".getBytes();
            BasicProperties basicProperties = new BasicProperties(deliverMode);
            channel.basicPublish("exchange","queue",basicProperties,body);

            Thread.sleep(500);
        }


    }
}
